#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswresample/swresample.h"
};

#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio

//Output PCM
#define OUTPUT_PCM 1


//void  fill_audio(void *udata,Uint8 *stream,int len){
    //SDL 2.0
    //SDL_memset(stream, 0, len);
    //if(audio_len==0)		[>  Only  play  if  we  have  data  left  <]
        //return;
    //len=(len>audio_len?audio_len:len);	[>  Mix  as  much  data  as  possible  <]

    //SDL_MixAudio(stream,audio_pos,len,SDL_MIX_MAXVOLUME);
    //audio_pos += len;
    //audio_len -= len;
//}
//-----------------

static char err_buf[128] = {0};
static char* av_get_err(int errnum)
{
    av_strerror(errnum, err_buf, 128);
    return err_buf;
}

int nWriteBytes=0;
int main(int argc, char* argv[])
{
    AVFormatContext	*pFormatCtx;
    int				i, audioStream;
    AVCodecContext	*pCodecCtx;
    AVCodec			*pCodec;
    AVCodecParserContext * pParser;

    char intput_file[256] = "test.mp3";

    av_register_all();
    pCodec = avcodec_find_decoder(AV_CODEC_ID_MP3/*AV_CODEC_ID_AAC*/);//AV_CODEC_ID_MP2
    if(pCodec==NULL){
        printf("Codec not found.\n");
        return -1;
    }

    pCodecCtx = avcodec_alloc_context3(pCodec);

    // Open codec
    if(avcodec_open2(pCodecCtx, pCodec, NULL)<0){
        printf("Could not open codec.\n");
        return -1;
    }

    //pParser = av_parser_init([>AV_CODEC_ID_AAC<]AV_CODEC_ID_MP3);
    pParser = av_parser_init(pCodec->id);
    if(!pParser){
        printf("av_parser_init AV_CODEC_ID_MP3 failed.\n");
        return -1;
    }

    FILE *pFile=NULL;
#if OUTPUT_PCM
    pFile=fopen("output.pcm", "wb");
#endif

    //Out Audio Param
    uint64_t out_channel_layout=AV_CH_LAYOUT_STEREO;
    int out_nb_samples=/*1024*/1152;
    AVSampleFormat out_sample_fmt=AV_SAMPLE_FMT_S16;
    int out_sample_rate=44100;
    int out_channels=av_get_channel_layout_nb_channels(out_channel_layout);
    //输出内存大小
    int out_buffer_size=av_samples_get_buffer_size(NULL,out_channels ,out_nb_samples,out_sample_fmt, 1);

    uint8_t *out_buffer=(uint8_t *)av_malloc(MAX_AUDIO_FRAME_SIZE*2);

    AVFrame *pFrame = av_frame_alloc();
    //avcodec_get_frame_defaults(pFrame);

    //SDL------------------
#if USE_SDL
    //Init
    wanted_spec.freq = out_sample_rate;
    wanted_spec.format = AUDIO_S16SYS;
    wanted_spec.channels = out_channels;
    wanted_spec.silence = 0;
    wanted_spec.samples = out_nb_samples/*pCodecCtx->frame_size*/;
    wanted_spec.callback = fill_audio;
    wanted_spec.userdata = pCodecCtx;
#endif
    ////printf("Bitrate:\t %3d\n", pFormatCtx->bit_rate);
    //printf("Decoder Name:\t %s\n", pCodecCtx->codec->long_name);
    //printf("Channels:\t %d\n", pCodecCtx->channels);
    //printf("Sample per Second\t %d \n", pCodecCtx->sample_rate);

    uint32_t ret,len = 0;
    int got_picture;
    int index = 0;
    struct SwrContext *au_convert_ctx;
    au_convert_ctx = swr_alloc();
    //au_convert_ctx=swr_alloc_set_opts(au_convert_ctx,out_channel_layout, out_sample_fmt, out_sample_rate,
    //	pCodecCtx->channel_layout,pCodecCtx->sample_fmt , pCodecCtx->sample_rate,0, NULL);
    //swr_init(au_convert_ctx);

    int data_size = 0;
    int size = 0;
    int cnt=0;
    int offset=0;

    //FILE *myout=fopen("output_log.txt","wb+");
    FILE *myout=stdout;

    unsigned char *aacframe=(unsigned char *)malloc(1024*5);
    unsigned char *aacbuffer=(unsigned char *)malloc(1024*1024);

    FILE *ifile = fopen(intput_file, "rb");
    if(!ifile){
        printf("Open file error");
        return -1;
    }

    printf(" aac 帧: NUM - Profile - Frequency - Size |\n");

    while(!feof(ifile)){
        data_size = fread(aacbuffer+offset, 1, 1024*1024-offset, ifile);
        unsigned char* input_data = aacbuffer+offset;

        uint8_t *pParserBufOut = NULL;
        int pParserOutLength = 0;

        while(data_size>0){
            int nParserLen = av_parser_parse2(pParser, pCodecCtx, &pParserBufOut, &pParserOutLength,
                    (uint8_t *)input_data, data_size,0, 0, 0);
            if(nParserLen>0){
                input_data += nParserLen;
                data_size  -= nParserLen;

                if(pParserOutLength>0){
                    AVPacket avpkt;
                    av_init_packet(&avpkt);
                    avpkt.data = (uint8_t *)pParserBufOut;
                    avpkt.size = pParserOutLength;

                    //此处不需要循环，每次解析的都是一个es流的一个单帧，直接解码成功
                    while(avpkt.size>0){
                        ret = avcodec_decode_audio4( pCodecCtx, pFrame,&got_picture, &avpkt);
                        if ( ret < 0 ) {
                            printf("Error in decoding audio frame.\n");
                            return -1;
                        }
                        //printf("avcodec_decode_audio4 %s \n", av_get_err(ret));
                        //if(AVERROR_INVALIDDATA==ret){
                        //	continue;
                        //}

                        //需要解码后初始化，否则pCodecCtx的参数尚未获取到（纯净版不用预先指定参数）
                        static bool bSwrInit=false;
                        if(!bSwrInit){
                            au_convert_ctx=swr_alloc_set_opts(au_convert_ctx,out_channel_layout, out_sample_fmt, out_sample_rate,
                                    pCodecCtx->channel_layout,pCodecCtx->sample_fmt , pCodecCtx->sample_rate,0, NULL);
                            swr_init(au_convert_ctx);
                            bSwrInit=true;
                        }

                        if(got_picture > 0){
                            int nBytes = swr_convert(au_convert_ctx,&out_buffer, MAX_AUDIO_FRAME_SIZE,(const uint8_t **)pFrame->data , pFrame->nb_samples);
                            if(nBytes<0){
                                printf("swr_convert failed \n");
                                return 0;
                            }
                            nWriteBytes = av_samples_get_buffer_size(NULL, out_channels, nBytes, out_sample_fmt, 1);
                            if(nWriteBytes<0){
                                printf("av_samples_get_buffer_size failed \n");
                                return 0;
                            }
                            fwrite(out_buffer, 1, nWriteBytes, pFile);
                        }

                        //设置音频数据缓冲,PCM数据
                        //audio_chunk = (Uint8 *) out_buffer;
                        //设置音频数据长度
                        //audio_len = nWriteBytes;
                        //audio_pos = audio_chunk;
                        //回放音频数据
                        //SDL_PauseAudio(0);
                        //while(audio_len>0)//等待直到音频数据播放完毕!
                            //SDL_Delay(1);

                        avpkt.data+=ret;
                        avpkt.size-=ret;
                    }
                    av_free_packet(&avpkt);
                }
            }
        }
    }
    fclose(ifile);
    free(aacbuffer);
    fclose(pFile);

    swr_free(&au_convert_ctx);
    av_free(out_buffer);
    avcodec_close(pCodecCtx);
    av_parser_close(pParser);

    return 0;
}

